package cn.cvs.servlet.supplier;

import cn.cvs.pojo.Supplier;
import cn.cvs.pojo.SysUser;
import cn.cvs.service.SupplierService;
import cn.cvs.util.SpringUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

@WebServlet(name = "SuppilerUpdateServlet", urlPatterns = "/supplier/update")
public class SuppilerUpdateServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet ( request, response );
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding ( "utf-8" );
        response.setContentType ( "text/html;charset=UTF-8" );
        SupplierService supplierService = (SupplierService) SpringUtil.getBean ( "supplierService" );
        Supplier supplier=new Supplier ();
        Integer integer = Integer.valueOf ( request.getParameter ( "id" ) );
        supplier.setId ( integer );
        supplier.setSupCode ( request.getParameter ( "supCode" ) );
        supplier.setSupName ( request.getParameter ( "supName" ) );
        supplier.setSupContact ( request.getParameter ( "supContact" ) );
        supplier.setSupPhone ( request.getParameter ( "supPhone" ) );
        supplier.setSupAddress ( request.getParameter ( "supAddress" ) );
        supplier.setSupFax ( request.getParameter ( "supFax" ) );
        supplier.setSupDesc ( request.getParameter ( "supDesc" ) );
        int update = supplierService.update ( supplier );
        SysUser userSession = (SysUser) request.getSession ( ).getAttribute ( "userSession" );
        supplier.setUpdatedUserId ( userSession.getId () );
        supplier.setUpdatedTime ( new Date (  ) );
        request.setAttribute ( "id",integer );
        if (update>0){
            request.getRequestDispatcher ( "/sys/supplier/list" ).forward ( request,response );
        }else {
            request.getRequestDispatcher ( "/sys/supplier/toUpdate" ).forward ( request,response );
        }
    }
}
